package com.app.config;//package com.app.config;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//
///**
// * Created by lijun on 2018/2/11.
// */
//
//@Configuration
//@EnableWebSecurity
//public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//
//    //    @Autowired
////    private UserService userService;
//    @Autowired
//    LoginSuccessHandler loginSuccessHandler;
//
//    @Autowired
//    LoginfailureHandler loginfailureHandler;
//
////@Autowired
////CustomAuthenticationFilter customAuthenticationFilter;
//
//    //再次访问index页面无需登录直接访问
//    //访问http://localhost:8080/home 不拦截，直接访问，
//    //访问http://localhost:8080/hello 需要登录验证后，且具备 “ADMIN”权限hasAuthority("ADMIN")才可以访问
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.csrf().disable()
////                .antMatchers("/home").permitAll()//访问：/home 无需登录认证权限
//                //.anyRequest().authenticated() //其他所有资源都需要认证，登陆后访问
////                .antMatchers("/hello").hasAuthority("ADMIN") //登陆后之后拥有“ADMIN”权限才可以访问/hello方法，否则系统会出现“403”权限不足的提示
//                //   .and()
//                //.addFilterAt(customAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)
//                .formLogin()
//                //.loginPage("/webapp/index.html#/pages/login")   ///webapp/index.html#/pages/login")//指定登录页是”/login”
//                .loginProcessingUrl("/api/login")//指定认证登录URL
//                .usernameParameter("loginname")//username
//                .passwordParameter("password")//password
//                .successHandler(loginSuccessHandler)//认证成功调取
//                .permitAll()
//                .successHandler(loginSuccessHandler) //登录成功后可使用loginSuccessHandler()存储用户信息，可选。
//                .failureHandler(loginfailureHandler)//认证失败调取
//                .and()
//                .logout()
//                .logoutSuccessUrl("/webapp/index.html") //退出登录后的默认网址是”/home”
//                .permitAll()
//
//                .invalidateHttpSession(true)
//                .and()
//                .authorizeRequests()
//                //  .antMatchers("/webapp/index.html#/pages/login" ).permitAll()
//                .antMatchers("/webapp/**.js",
//                        "/webapp/**.js.map",
//                        "**/**.ico",
//                        "**/**.eot", "**/**.woff**", "**/**.eot", "**/**.svg",
//                        "**/**.ttf",
//                        "**/assets/**", "/api/login/**","/api/**",
//                        "/webapp/#/pages/login",
//                        "/webapp/index.html#/pages/login",
//                        "/webapp/webapp/index.html#/pages/login",
//                        "/test",
//                        "/DemoService/post/**/**",
//                        // "**/**.json",
//                        //"**/**.css",
//
//                        "**/favicon.ico" )
//                .permitAll()
//                //.antMatchers("/webapp/index.html#/**").hasAuthority("admin")
//               .antMatchers("/**").hasRole("ADMIN")
//                .anyRequest().authenticated();// .and()
//              //  .addFilter(new JWTLoginFilter(authenticationManager()))
//             //   .addFilter(new JWTAuthenticationFilter(authenticationManager()));
//
//
//        ;
////                .and()
////                .rememberMe()//登录后记住用户，下次自动登录,数据库中必须存在名为persistent_logins的表
//        //                .tokenValiditySeconds(1209600);
//        //用重写的Filter替换掉原有的UsernamePasswordAuthenticationFilter
//
//        http.sessionManagement()//Session管理器
//               // .sessionFixation().changeSessionId()
//                .sessionAuthenticationErrorUrl("/webapp/index.html#/pages/login")
//                .invalidSessionUrl("/webapp/index.html#/pages/login")//Session失效
//                .maximumSessions(1)//只能同时一个人在线
//                .expiredUrl("/webapp/index.html#/pages/login");
//
//
//
//       // http
//        //        .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class)
//
//
//    }
//
//
//
////    @Autowired
////    private UserDetailsService userDetailsService;
//////    @Autowired
//////    private BCryptPasswordEncoder bCryptPasswordEncoder;
////
//////    public WebSecurityConfig(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
//////        this.userDetailsService = userDetailsService;
//////        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
//////    }
////
////    @Override
////    public void configure(AuthenticationManagerBuilder auth) throws Exception {
////        auth.userDetailsService(userDetailsService);//.passwordEncoder(bCryptPasswordEncoder);
////    }
////    @Bean
////    CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
////    	CustomAuthenticationFilter customAuthenticationFilter = new CustomAuthenticationFilter();
////        //这句很关键，重用WebSecurityConfigurerAdapter配置的AuthenticationManager，不然要自己组装AuthenticationManager
////    	customAuthenticationFilter.setAuthenticationManager(authenticationManager());
////    	customAuthenticationFilter.setAuthenticationSuccessHandler(loginSuccessHandler);
////    	customAuthenticationFilter.setAuthenticationFailureHandler(loginfailureHandler);
////    	customAuthenticationFilter.setFilterProcessesUrl("/api/login");
////
////      return customAuthenticationFilter;
////    }
//
////
////    @Autowired
////    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//////指定密码加密所使用的加密器为passwordEncoder()
//////需要将密码加密后写入数据库
////        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
////        auth.eraseCredentials(false);
////    }
////
////    @Bean
////    public BCryptPasswordEncoder passwordEncoder() {
//
////        return new BCryptPasswordEncoder(4);
////    }
////
////    @Bean
////    public LoginSuccessHandler loginSuccessHandler(){
////        return new LoginSuccessHandler();
////    }
//}
